package com.ruoyi.personnel.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.personnel.domain.Personnel;
import com.ruoyi.personnel.mapper.PersonnelMapper;
import com.ruoyi.personnel.service.IPersonnelService;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.validation.Validator;
import java.util.List;
import java.util.Objects;

/**
 * 人口/人员信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-29
 */
@Service
public class PersonnelServiceImpl implements IPersonnelService 
{
    @Autowired
    private PersonnelMapper personnelMapper;

    @Autowired
    protected Validator validator;

    private static final Logger log = LoggerFactory.getLogger(PersonnelServiceImpl.class);
    private String msg;

    /**
     * 插入表格数据
     *
     * @param personnelList
     * @param updateSupport
     * @param operName
     * @return successMSG
     */
    @Override
    public String importUser(List<Personnel> personnelList, boolean updateSupport, String operName) {
        if(CollectionUtils.isEmpty(personnelList))
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int SuccessNum = 0;
        int FailNum = 0;
        StringBuilder SuccessMsg = new StringBuilder();
        StringBuilder FailMsg = new StringBuilder();
        for(Personnel personnel : personnelList)
        {
            try
            {
                Personnel existingPersonnel = personnelMapper.selectPersonnelByIDcard(personnel.getpNum());
                if(Objects.isNull(existingPersonnel))
                {
                    BeanValidators.validateWithException(validator, personnel);
                    personnel.setCreateBy(operName);
                    System.out.println("**************************************************"+personnel.toString());
                    this.insertPersonnel(personnel);
                    SuccessNum++;
                    SuccessMsg.append("<br/>" + SuccessNum + "、项目编号 " + personnel.getpNum() + " 导入成功");
                }
                else if(updateSupport)
                {
                    BeanValidators.validateWithException(validator, personnel);
                    personnel.setCreateBy(operName);
                    System.out.println("**************************************************"+personnel.toString());
                    this.updatePersonnel(personnel);
                    SuccessNum++;
                    SuccessMsg.append("<br/>" + SuccessNum + "、项目编号 " + personnel.getpNum() + " 更新成功");
                }
                else
                {
                    FailNum++;
                    FailMsg.append("<br/>" + FailNum + "、项目编号 " + personnel.getpName() + " 已存在");
                }
            }catch (Exception e)
            {
                FailNum++;
                FailMsg.append("<br/>" + FailNum + "、项目编号 " + personnel.getpNum() + " 导入失败：" + e.getMessage());
                log.error(()->msg,e);
            }
        }
        if(FailNum>0)
        {
            FailMsg.insert(0, "很抱歉，导入失败！共 " + FailNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(FailMsg.toString());
        }
        else
        {
            SuccessMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + SuccessNum + " 条，数据如下：");
        }
        return SuccessMsg.toString();
    }

    @Override
    public Personnel selectPersonnelByIDcard(String pNum) {
        return personnelMapper.selectPersonnelByIDcard(pNum);
    }

    @Override
    public int updatePersonnelByIdCard(String pNum){
        return personnelMapper.updatePersonnelByIdCard(pNum);
    }


    /**
     * 查询人口/人员信息
     * 
     * @param pId 人口/人员信息主键
     * @return 人口/人员信息
     */
    @Override
    public Personnel selectPersonnelByPId(Long pId)
    {
        return personnelMapper.selectPersonnelByPId(pId);
    }

    /**
     * 查询人口/人员信息列表
     * 
     * @param personnel 人口/人员信息
     * @return 人口/人员信息
     */
    @Override
    public List<Personnel> selectPersonnelList(Personnel personnel)
    {
        return personnelMapper.selectPersonnelList(personnel);
    }

    /**
     * 新增人口/人员信息
     * 
     * @param personnel 人口/人员信息
     * @return 结果
     */
    @Override
    public int insertPersonnel(Personnel personnel)
    {
        personnel.setCreateTime(DateUtils.getNowDate());
        return personnelMapper.insertPersonnel(personnel);
    }

    /**
     * 修改人口/人员信息
     * 
     * @param personnel 人口/人员信息
     * @return 结果
     */
    @Override
    public int updatePersonnel(Personnel personnel)
    {
        personnel.setUpdateTime(DateUtils.getNowDate());
        return personnelMapper.updatePersonnel(personnel);
    }

    /**
     * 批量删除人口/人员信息
     * 
     * @param pIds 需要删除的人口/人员信息主键
     * @return 结果
     */
    @Override
    public int deletePersonnelByPIds(Long[] pIds)
    {
        return personnelMapper.deletePersonnelByPIds(pIds);
    }

    /**
     * 删除人口/人员信息信息
     * 
     * @param pId 人口/人员信息主键
     * @return 结果
     */
    @Override
    public int deletePersonnelByPId(Long pId)
    {
        return personnelMapper.deletePersonnelByPId(pId);
    }


}
